﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace WebApplication1
{
    public partial class reg : System.Web.UI.Page
    {
        string no = null;
        SqlConnection cnn = new SqlConnection(WebConfigurationManager.AppSettings["connect"]);
        protected void Page_Load(object sender, EventArgs e)
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand("select top 1 no from regstr order by no desc", cnn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows == false)
                txtno.Text = "100";
            else
            {
                while (dr.Read())
                {
                    no = Convert.ToString(Convert.ToInt32(dr["no"]) + 1);
                    txtno.Text = Convert.ToString(no);
                }
            }
            cnn.Close();
        }
        
        protected void regbtn_Click(object sender, EventArgs e)
        {
            cnn.Open();
            
            //insert without last name
            if (txtlname.Text == "")
            {
                SqlCommand cmd = new SqlCommand("insert into regstr(no,fname,uname,pass,cno,gen,mail,dob,addr) values('" + txtno.Text + "','" + txtfname.Text + "','" + txtuname.Text + "','" + txtpass.Text + "','" + txtcno.Text + "','" + female.Text + "','" + txtmail.Text + "','" + TextBox1.Text + "','" + txtaddr.Text + "')", cnn);
                SqlCommand cmd1 = new SqlCommand("insert into account(accno,name) values('" + txtno.Text + "','" + txtfname.Text + "')", cnn);
                cmd.ExecuteNonQuery();
                cmd1.ExecuteNonQuery();
            }
            else 
            {
                //insert with last name
                SqlCommand cmd = new SqlCommand("insert into regstr(no,fname,lname,uname,pass,cno,gen,mail,dob,addr) values('" + txtno.Text + "','" + txtfname.Text + "','" + txtlname.Text + "','" + txtuname.Text + "','" + txtpass.Text + "','" + txtcno.Text + "','" + female.Text + "','" + txtmail.Text + "','" + TextBox1.Text + "','" + txtaddr.Text + "')", cnn);
                SqlCommand cmd1 = new SqlCommand("insert into account(accno,name) values('" + txtno.Text + "','" + txtfname.Text + "')", cnn);
                cmd.ExecuteNonQuery();
                cmd1.ExecuteNonQuery();
            }
            cnn.Close();
            Session.Add("no",txtno.Text);
            Session.Add("name",txtuname.Text);
            Response.Redirect("~/Users/usracc.aspx");
        }

        protected void btnclear_Click(object sender, EventArgs e)
        {
            txtaddr.Text = "";
            txtcno.Text = "";
            txtfname.Text = "";
            txtlname.Text = "";
            txtmail.Text = "";
            txtno.Text = "";
            txtpass.Text = "";
            txtuname.Text = "";
            TextBox1.Text = "";
            male.Checked = false;
            female.Checked = false;
        }

        protected void btncancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/log.aspx");
        }
    }
}